﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
namespace BitPortal.Admin.Product.ProductProperty.Module
{
    public partial class ProductPropertyAddEdit : Controls.BaseAdminControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            mtslCategory.Items.Clear();
            BindCategory(null, "");

        }
        protected void btnInsert_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductEdit");
                RichEditor1.DataType = cbType.SelectedItem.Value;
                //string appliedCategories = "";
                //if (TreeProductCategory1.TreepanelCategory.CheckedNodes != null)
                //{
                //    appliedCategories = ProductPropertyBLL.GenerateCategory(TreeProductCategory1.TreepanelCategory.CheckedNodes.Select(ent => ent.NodeID).ToArray());
                //}

                Models.ProductProperty property = new Models.ProductProperty()
                {
                    Name = txtName.Text,
                    DataType = cbType.SelectedItem.Value,
                    DefaultValue = RichEditor1.Value,
                    ApplyToAllProduct = ckApplyToAllProduct.Checked,
                    IsEnabled = ckIsEnabled.Checked,
                    AllowAdvanceSearch = ckAllowAdvanceSearch.Checked,
                    AllowFilter = ckAllowFilter.Checked,
                    AppliedCatgoryID = ProductPropertyBLL.GenerateCategory(mtslCategory.SelectedItems.Select(ent => ent.Value).ToArray())
                };
                if (property.Insert() > 0)
                {
                    (this.Page as Controls.BaseAdminPage).ShowNotification("Thêm thuộc tính sản phẩm thành công");
                    Window1.Hide();
                    ReloadTargetData();
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        protected void btnUpdate_Click(object sender, DirectEventArgs e)
        {
            try
            {
                PermissionBLL.CheckRoleThrowException("ProductEdit");
                ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
                int id = Utils.GeneralUtils.ToInt32TryParse(txtID.Text);
                Models.ProductProperty p = bitEnt.ProductProperties.SingleOrDefault(ent => ent.PropertyID == id);
                if (p != null)
                {
                    RichEditor1.DataType = cbType.SelectedItem.Value;
                    //string appliedCategories = "";
                    //if (TreeProductCategory1.TreepanelCategory.CheckedNodes != null && TreeProductCategory1.TreepanelCategory.CheckedNodes.Count > 0)
                    //{
                    //    appliedCategories = ProductPropertyBLL.GenerateCategory(TreeProductCategory1.TreepanelCategory.CheckedNodes.Select(ent => ent.NodeID).ToArray());
                    //}

                    p.Name = txtName.Text;
                    p.DataType = cbType.SelectedItem.Value;
                    p.DefaultValue = RichEditor1.Value;
                    p.ApplyToAllProduct = ckApplyToAllProduct.Checked;
                    p.IsEnabled = ckIsEnabled.Checked;
                    p.AllowAdvanceSearch = ckAllowAdvanceSearch.Checked;
                    p.AllowFilter = ckAllowFilter.Checked;

                    p.AppliedCatgoryID = ProductPropertyBLL.GenerateCategory(mtslCategory.SelectedItems.Select(ent => ent.Value).ToArray());

                    if (bitEnt.SaveChanges() > 0)
                    {
                        (this.Page as Controls.BaseAdminPage).ShowNotification("Sửa thuộc tính sản phẩm thành công");
                        Window1.Hide();
                        ReloadTargetData();
                    }
                }
            }
            catch (Exception ex)
            {
                (this.Page as Controls.BaseAdminPage).ShowMessage(ex.Message);
            }
        }

        public void BindData(int? propertyID = null)
        {
            if (propertyID == null)
            {
                txtID.Text = "0";

                txtName.Text = "";
                ckAllowAdvanceSearch.Checked = ckAllowFilter.Checked = ckApplyToAllProduct.Checked = ckIsEnabled.Checked = true;
                RichEditor1.DataType = "String";
                cbType.SetValueAndFireSelect("String");
                RichEditor1.Value = "";

                btnUpdate.Hidden = true;
                btnInsert.Hidden = false;
                Window1.Show();
                return;
            }
            Models.ProductProperty p = Business.ProductPropertyBLL.GetByID((int)propertyID);
            if (p != null)
            {
                txtID.Text = p.PropertyID.ToString();

                txtName.Text = p.Name;
                cbType.SelectedItem.Value = RichEditor1.DataType = p.DataType;
                RichEditor1.Value = p.DefaultValue;
                ckApplyToAllProduct.Checked = p.ApplyToAllProduct;
                ckIsEnabled.Checked = p.IsEnabled;
                ckAllowAdvanceSearch.Checked = p.AllowAdvanceSearch;
                ckAllowFilter.Checked = p.AllowFilter;

                mtslCategory.SelectedItems.Clear();
                foreach (string str in ProductPropertyBLL.ReturnCategory(p.AppliedCatgoryID))
                {
                    mtslCategory.SelectedItems.Add(new SelectedListItem(str));
                }
                mtslCategory.UpdateSelection();

                btnUpdate.Hidden = false;
                btnInsert.Hidden = true;
                Window1.Show();
            }
        }

        [DirectMethod]
        public void ChangeEdit(string value)
        {
            RichEditor1.DataType = value;
        }

        protected void BindCategory(ProductCategory parent, string prefix)
        {
            foreach (BitPortal.Models.ProductCategory ac in ProductCategoryBLL.GetChilds(parent))
            {
                mtslCategory.Items.Add(new Ext.Net.ListItem(prefix + " " + ac.Name, ac.CategoryID.ToString()));
                BindCategory(ac, prefix + "----");
            }
        }

    }
}